package com.magpiebridge.controller;

import com.magpiebridge.pojo.Miss;
import com.magpiebridge.service.MissService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
public class MissController {
    @Autowired
    private MissService missService;

    @GetMapping("/getAllMiss")
    public List<Miss> getAllMiss(){
        return missService.getAllMiss();
    }

    @GetMapping("/getMissById/{id}")
    public Miss getMissById (@PathVariable Integer id){
        return missService.getMissById(id);
    }
    @PostMapping("/insertMiss")
    public String insertMiss(@RequestBody Miss miss){
        Integer missid = miss.getMissid();
        Integer sponsor = miss.getSponsor();
        Integer Invited = miss.getInvited();
        String address = miss.getAddress();
        Date misstime = miss.getMisstime();
        String missstate = miss.getMissstate();

        int result = missService.insertMiss(miss);
        if (result > 0){
            return "约会添加成功";
        } else {
            return "约会添加失败";
        }
    }

    @DeleteMapping("/deleteMiss/{id}")
    public  String deleteMiss(@PathVariable  Integer  id)  {
        try  {
            //  查询约会是否存在
             Miss miss  =  missService.getMissById(id);
            if  (miss  ==  null)  {
                return  "约会不存在";
            }
            //  删除约会
            int  result  =  missService.deleteMiss(id);
            if  (result  >  0)  {
                //  更新数据库状态
                return  "约会删除成功";
            }  else  {
                return  "约会删除失败";
            }
        }  catch  (Exception  e)  {
            //  记录日志或警告
            //  TODO:  Log  deletion  information
            // 打印错误信息
            System.err.println(e.getMessage());
            return "系统错误";
        }
    }

    @PostMapping("/updateMiss/{id}")
    public String updateMiss(@PathVariable Integer id, @RequestBody Miss newMiss) {
        try {
            // 查询约会是否存在
            Miss miss = missService.getMissById(id);
            if (miss == null) {
                return "约会不存在";
            }
            // 更新用户信息
            miss.setMissid(newMiss.getMissid());
            miss.setSponsor(newMiss.getSponsor());
            miss.setInvited(newMiss.getInvited());
            miss.setAddress(newMiss.getAddress());
            miss.setMisstime(newMiss.getMisstime());
            miss.setMissstate(newMiss.getMissstate());

            int result = missService.updateMiss(miss);
            if (result > 0) {
                return "约会更新成功";
            } else {
                return "约会更新失败";
            }
        } catch (Exception e) {
            // 记录日志或警告
            // TODO: Log update information
            // 打印错误信息
            System.err.println(e.getMessage());
            return "系统错误";
        }
    }
}
